using System;
using System.Text;
using System.Windows.Forms;
using System.Collections.Generic;
using Shuriken.Core.Utils; 


namespace Shuriken.Core.UI.Controls
{
    /// <summary>
    /// System wide hotkey article: 
    /// http://www.vbaccelerator.com/home/NET/Code/Libraries/Windows_Messages/Hot_Key_Form/article.asp
    /// </summary>
    public class HotkeyEditor : System.Windows.Forms.TextBox
    {
        private System.Drawing.Color m_SelectionColor;
        private System.Drawing.Color m_UnselectedColor;
        private System.Windows.Forms.Keys m_Shortcut;

        /// <summary>
        /// Default constructor
        /// </summary>
        public HotkeyEditor()
            : base()
        {
        }

        #region TextBox overrides

        protected override void OnKeyDown(System.Windows.Forms.KeyEventArgs e)
        {
            System.Diagnostics.Debug.Write("\n OnKeyDown [" +
                "KeyCode: " + e.KeyCode.ToString() + 
                ", KeyData: " + e.KeyData.ToString() + 
                ", Modifiers: " + e.Modifiers.ToString() + 
                ", KeyValue: " + e.KeyValue.ToString() + "].", "Hotkey Editor");

            Text = KeyEventArgsToString(e);

            if (IsShortCut(e))
            {
                m_Shortcut = e.KeyData;
                string f = KeyEventArgsToString(e);
                //System.Diagnostics.Trace.WriteLine("Shortcut changed : " + e.KeyData.ToString() + " , #modifier: " + modifiers + " , #kdata: " + kdata + " , #kcode: " + kcode);
            }

            e.Handled = true;
            //base.OnKeyDown(e);
        }

        /// <summary>
        /// True if the key args are in a form of modifier-key
        /// </summary>
        /// <param name="key">key</param>
        /// <returns></returns>
        private bool IsShortCut(System.Windows.Forms.KeyEventArgs key)
        {
            if ((key.KeyCode != Keys.Menu) &&
                (key.KeyCode != Keys.LWin) &&
                (key.KeyCode != Keys.ControlKey) &&
                (key.KeyCode != Keys.Alt) &&
                (key.KeyCode != Keys.ShiftKey) &&
                (key.KeyCode != Keys.Apps))
            {
                return true;
            }

            return false;
        }

        private string KeyEventArgsToString(System.Windows.Forms.KeyEventArgs e)
        {
            string[] keys;
            
            if (IsShortCut(e))
            {
                keys = e.KeyData.ToString().Split(new string[] { ", " }, StringSplitOptions.RemoveEmptyEntries);
            }
            else
            {
                keys = e.Modifiers.ToString().Split(new string[] { ", " }, StringSplitOptions.RemoveEmptyEntries);
            }

            string result = string.Empty;

            for (int loop = keys.Length; loop > 0; loop--)
            {
                result += keys[loop - 1];
                if (loop > 1)
                {
                    result += "-";
                }
            }

            return result;
        }

        protected override void OnKeyUp(System.Windows.Forms.KeyEventArgs e)
        {
            Tracer.WriteVerbose(
               "\n OnKeyUp [" + 
               "KeyCode: " + e.KeyCode.ToString() + 
               ", KeyData: " + e.KeyData.ToString() + 
               ", KeyValue: " + e.KeyValue.ToString() + "].","Hotkey Editor");

            e.Handled = true;
            // base.OnKeyUp(e);
        }

        protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
        {
            //System.Diagnostics.Trace.Write("\n InputForm_KeyPress [");
            //System.Diagnostics.Trace.Write("KeyChar: " + e.KeyChar + "].");
            e.Handled = true;
            //base.OnKeyPress(e);
        }

        /// <summary>
        /// Receiving focus, highlighting it by changing the fore color. 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnGotFocus(EventArgs e)
        {
            m_UnselectedColor = base.BackColor;
            base.BackColor = m_SelectionColor;

            base.OnGotFocus(e);
        }

        /// <summary>
        /// Lost focus, reverting to the original fore color. 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLostFocus(EventArgs e)
        {
            base.BackColor = m_UnselectedColor;
            base.OnLostFocus(e);
        }

        #endregion

        #region Properties
        public System.Drawing.Color SelectionColor
        {
            get
            {
                return m_SelectionColor;
            }
            set
            {
                m_SelectionColor = value;
            }
        }

        /// <summary>
        /// Storing the selected key combination.
        /// </summary>
        public System.Windows.Forms.Keys Shortcut
        {
            get
            {
                return m_Shortcut; 
            }
            set
            {
                m_Shortcut = value;

                KeyEventArgs kea = new KeyEventArgs(value);
                Text = KeyEventArgsToString(kea);
            }
        }
        #endregion
    }
}
